<template>
  <el-container>
    <el-header>
      <div class="report_header" ref="headerHeight">
        <div class="report_header_left"></div>
        <div style="width: 100px;display: flex;align-items: center;margin-left: 10px;">
          <svg-icon class="primary" :icon-class="$route.meta.icon"/>
          <span style="margin-left: 5px;" class="primary">{{$route.meta.title}}</span>
        </div>
        <div style="display: flex;flex-direction: row;justify-content: flex-end;width: 100%;">
          <div style="display: flex;flex-direction: row;flex-wrap: wrap;overflow: hidden;height:41px;justify-content: flex-end;">
            <div class="report_header_date">
              <el-date-picker
                style="width: 100%"
                v-model="query.date"
                value-format="yyyy-MM-dd"
                type="daterange"
                align="right"
                unlink-panels
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="pickerOptions">
              </el-date-picker>
            </div>
            <div class="report_header_query" v-if="mchType==0||mchType==3">
              <el-select v-model="query.sysMchId" clearable placeholder="请选择商户"
                         filterable  value-key="id" :filter-method="querySysmch" @change="changeSysMch" >
                <el-option v-for="item in mchIdOptions" :key="item.id"   :label="item.name" :value="item.id">
                </el-option>
              </el-select>
            </div>
            <div class="report_header_query" >
              <el-input prefix-icon="el-icon-search" placeholder="配送单号"
                        v-model="query.sn" style="width: 100%"></el-input>
            </div>
            <div class="report_header_query">
              <el-select v-model="supplierName" @change="changeSupp" placeholder="请选择供应商" value-key="id"  clearable filterable :filter-method="remoteMethod" style="width: 100%">
                <el-option v-for="(item,index) in suppliers"   style="width: 600px" :label="item.name" :value="item" :key="item.id">
                  <div style="display: flex">
                    <span style="float: left;width: 80px;overflow: hidden;text-overflow: ellipsis;">{{ item.name }}</span>
                    <span style="width: 100px;overflow: hidden;text-overflow: ellipsis;">{{ item.phone }}</span>
                    <div style="width: 200px;">
                      <span style="color: #8492a6; font-size: 8px;">{{item.areaName}}{{item.address}}</span>
                    </div>
                  </div>
                </el-option>
                <div style="display: flex;width: 100%;position: absolute;bottom: 0">
                  <div style="width: 50%;background-color: rgb(248,248,248);"></div>
                  <div class="elOption" @click="toSearchSupplier">搜索供应商</div>
                </div>
                <div style="height: 30px"></div>
              </el-select>
            </div>
            <div class="report_header_query" >
              <el-select v-model="memberName" value-key="id" clearable placeholder="请选择客户"
                         filterable @focus="getMemberList('')"  @change="selectMember"  :filter-method="queryMember">
                <el-option v-for="item in members" style="width: 600px;"  :key="item.id" :label="item.name"
                           :value="item">
                  <div style="display: flex">
                    <span style="width: 60px;">{{item.code}}</span>
                    <span style="float: left;width: 100px;overflow: hidden;text-overflow: ellipsis;">{{ item.name }}</span>
                    <span style="float: right; color: #8492a6; font-size: 8px;width: 80px;margin-left: 10px">{{ item.mobile }}</span>
                    <div style="width: 200px;">
                      <span style="color: #8492a6; font-size: 8px;">{{item.address}}</span>
                    </div>
                  </div>
                </el-option>
                <div style="display: flex;width: 100%;position: absolute;bottom: 0">
                  <div style="width: 50%;background-color: rgb(248,248,248);"></div>
                  <div class="elOption" @click="toSearchMember">搜索客户</div>
                </div>
                <div style="height: 30px"></div>
              </el-select>
            </div>
          </div>
          <el-button style="margin-left: 10px;margin-top: 5px;" size="small" type="primary" @click="queryClick">查询</el-button>
        </div>
      </div>
    </el-header>
    <div class="pd10">
      <el-tabs type="border-card" @tab-click="tabsClick" :style="{height:curHeight+'px'}">
        <el-tab-pane label="全部">
          <div class="details_card" v-loadMore="[payLoads,'returns-list0']" :style="{height:tabsHeight+'px'}">
            <returnslist :returnsForm="returnsForm" @detailsClick="detailsClick"></returnslist>
          </div>
        </el-tab-pane>
        <el-tab-pane label="待确认">
          <div class="details_card" v-loadMore="[payLoads,'returns-list1']" :style="{height:tabsHeight+'px'}">
            <returnslist :returnsForm="returnsForm" @detailsClick="detailsClick"></returnslist>
          </div>
        </el-tab-pane>
        <el-tab-pane label="已退货">
          <div class="details_card" v-loadMore="[payLoads,'returns-list2']" :style="{height:tabsHeight+'px'}">
            <returnslist :returnsForm="returnsForm" @detailsClick="detailsClick"></returnslist>
          </div>
        </el-tab-pane>
        <el-tab-pane label="已完成">
          <div class="details_card" v-loadMore="[payLoads,'returns-list3']" :style="{height:tabsHeight+'px'}">
            <returnslist :returnsForm="returnsForm" @detailsClick="detailsClick"></returnslist>
          </div>
        </el-tab-pane>
        <el-tab-pane label="已取消">
          <div class="details_card" v-loadMore="[payLoads,'returns-list4']" :style="{height:tabsHeight+'px'}">
            <returnslist :returnsForm="returnsForm" @detailsClick="detailsClick"></returnslist>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
    <el-dialog
      title="双击选择客户"
      :visible.sync="memberTableVisible"
      width="950px"
      v-el-drag-dialog
      append-to-body
      :before-close="memberTableClose">
      <div style="position: relative;">
        <member-table :key="times" @memberDouble="memberDouble" @memberTableClose="memberTableClose"></member-table>
      </div>
    </el-dialog>
    <el-dialog
      title="查找供应商"
      :visible.sync="supplierVisible"
      width="950px"
      v-el-drag-dialog
      append-to-body
      :before-close="supplierClose">
      <div style="position: relative;height: 63vh;overflow: auto">
        <supplierTable :key="times"  @doubleSupplier="doubleSupplier"></supplierTable>
      </div>
    </el-dialog>
    <el-footer class="el-footerBox">
      <pagination :total="total" :page.sync="pageNum" :limit.sync="pageSize"
                  @pagination="loadOrder" style="margin-bottom: auto"></pagination>
    </el-footer>
  </el-container>
</template>
<script>
  import elDragDialog from '@/directive/el-drag-dialog' // base on element-ui
  import memberTable from '@/components/MemberTable/index'
  import supplierTable from '../../components/SupplierTable/supplierList'
  import {list as supplierList} from '@/api/supplier'
  import loadMore from '@/utils/more.js'
  import {EventBus} from '@/api/eventBus'
  import returnslist from './components/list'
  import {list} from '@/api/return'
  import Pagination from '@/components/Pagination';
  import utils from '../../utils/utils.js'
  import { list as memberList } from '@/api/member'
  import { getInfo, list as sysMchList } from '@/api/sysMch'
  export default {
    name: 'returns-list',
    directives: {loadMore,elDragDialog},
    activated() {
      this.payLoads = new Date().getTime();
    },
    data() {
      return {
        payLoads:'',
        memberName:'',
        returnsForm: [],
        memberTableVisible:false,
        pageNum: 1,
        pageSize: 20,
        supplierName:'',
        total: 0,
        curHeight: null,
        mchIdOptions:[],
        mchType:'',
        supplierVisible:false,
        times:'',
        tabsHeight:null,
        queryType: true,
        suppliers:[],
        members:[],
        shippingStatus: '',
        query: {
          date: [],
          sysMchId:'',
          memberId:'',
          supplierId:'',
          sn: ''
        },
        keyword: {
          supplierId:'',
          memberId:'',
          date: [],
          sn: ''
        },
        pickerOptions: {//时间快捷选择
          shortcuts: [{
            text: '昨天',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
      };
    },
    components: {
      returnslist, Pagination,memberTable,supplierTable
    },
    created() {
      var _this = this
      this.loadSysmch('')
      this.loadOrder()
      this.loadMch()
      this.loadSupplier('')
      EventBus.$on('warehouse-add', res => { // 监听新增完毕
        // 更新列表数组
        _this.returnsForm.splice(0, 0, res)
      });
      EventBus.$on('warehouse-view', res => { // 监听新增完毕
        // 更新列表数组
        for (var i = 0; i < _this.returnsForm.length; i++) {
          if (_this.returnsForm[i].id == res.id) {
            this.$set(this.returnsForm, i, res)
          }
        }
      })
      EventBus.$on('returns-view', res => { // 监听新增完毕
        // 更新列表数组
        for (var i = 0; i < _this.returnsForm.length; i++) {
          if (_this.returnsForm[i].id == res.id) {
            this.$set(this.returnsForm, i, res)
          }
        }
      })
    },
    mounted() {
      this.comeIn();
      this.beforeMount()
    },
    methods: {
      loadSysmch(query){
        sysMchList({ name: query }).then(res => {
          this.mchIdOptions = res.data.data.filter(val => { return val.mchType === 3||val.mchType === 1 })
        }).catch(err => { console.log(err) })
      },
      changeSysMch(){
        this.loadOrder()
      },
      querySysmch(query){
        this.loadSysmch(query)
      },
      loadMch(){
        getInfo().then(res=>{
          this.mchType=res.data.mchType
        })
      },
      doubleSupplier(data){
        this.supplierName=data.name
        this.query.supplierId=data.id
        this.supplierVisible=false
      },
      supplierClose(){
        this.supplierVisible=false
      },
      memberDouble(data){
        this.memberName=data.name
        this.query.memberId=data.id
        this.memberTableVisible = false
      },
      memberTableClose(){
        this.memberTableVisible = false
      },
      toSearchMember() {
        this.memberTableVisible = true
      },
      selectMember(data){
        if(!utils.isNull(data)){
          this.memberName=data.name
          this.query.memberId=data.id
        }else{
          this.query.memberId=''
        }
      },
      toSearchSupplier(){
        this.times=new Date().getSeconds()
        this.supplierVisible=true
      },
      changeSupp(val){
        if(!utils.isNull(val)){
          this.supplierName=val.name
          this.query.supplierId=val.id
        }else{
          this.query.supplierId=''
        }

      },
      queryMember(query){
        this.getMemberList(query)
      },
      getMemberList(query){
        let param=new URLSearchParams()
        param.append('keyword',query)
        memberList(param).then(res=>{
          this.members=res.data.data
        })
      },
      loadSupplier(query){
        let params=new URLSearchParams()
        params.append('name',query)
        supplierList(params).then(res=>{
          this.suppliers=res.data.data
        })
      },
      beforeMount() {
        var _this = this
        window.onresize = () => {
          return (() => {
            if (_this.queryType == false) {
              var h = document.documentElement.clientHeight || document.body.clientHeight;
              _this.curHeight = h - 270//减去页面上固定高度height
              this.tabsHeight = h - 340
            } else {
              var h = document.documentElement.clientHeight || document.body.clientHeight;
              _this.curHeight = h - 224//减去页面上固定高度height
              this.tabsHeight = h - 294
            }
          })();
        };
      },
      comeIn() {
        console.log('222')
        var h = document.documentElement.clientHeight || document.body.clientHeight;
        this.curHeight = h - 224//减去页面上固定高度height
        this.tabsHeight = h - 294
      },
      loadOrder() {
        var _this = this;
        if(utils.isNull(this.keyword.date)){
          this.keyword.date = []
        }
        let params = new URLSearchParams();
        params.append('pageNum', _this.pageNum);
        params.append('pageSize', _this.pageSize);
        params.append('sn', _this.keyword.sn);
        params.append('memberId',_this.keyword.memberId)
        params.append('supplierId',_this.keyword.supplierId)
        params.append('mchId',this.query.sysMchId)
        params.append('shippingStatus',_this.shippingStatus)
        params.append('startDate', this.keyword.date.length > 0 ? this.keyword.date[0] : '');
        params.append('endDate', this.keyword.date.length > 0 ? this.keyword.date[1] : '');
        list(params).then(response => {
          console.log(response)
          _this.returnsForm = response.data.data
          _this.total = response.data.total
        }).catch(error => {
          console.log('error!!')
        });
      },
      chooseClick() {
        this.queryType = !this.queryType
        if (this.queryType == false) {
          var h = document.documentElement.clientHeight || document.body.clientHeight;
          this.curHeight = h - 270//减去页面上固定高度height
          this.tabsHeight = h - 340
        } else {
          var h = document.documentElement.clientHeight || document.body.clientHeight;
          this.curHeight = h - 224//减去页面上固定高度height
          this.tabsHeight = h - 294
        }
      },
      remoteMethod(query){
        this.loadSupplier(query)
      },
      queryClick() {
        this.keyword = this.query;
        this.pageNum = 1;
        this.loadOrder()
      },
      resetClick() {
        this.memberName=''
        this.supplierName=''
        this.query.date = [];
        this.query.sn = '';
        this.query.supplierId=''
        this.query.memberId=''
        this.keyword = this.query;
        this.pageNum = 1;
        this.loadOrder()
      },
      detailsClick(data) {
        this.$router.push({
          path: '/returns/view', query: {id: data}
        })
      },
      tabsClick(data) {
        var _this = this
        console.log(data)
        switch (data.paneName) {
          case '0':
            _this.shippingStatus = ''; // 配送状态
            break;
          case '1':
            console.log(data.paneName)
            _this.shippingStatus = 1; // 配送状态
            break;
          case '2':
            _this.shippingStatus = 3; // 配送状态
            break;
          case '3':
            _this.shippingStatus = 4; // 配送状态
            break;
          case '4':
            _this.shippingStatus = 5; // 配送状态
            break;
        }
        this.loadOrder()
      }
    }
  }
</script>

<style scoped>
  .elOption{
    background-color: #909399;
    color:white ;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 50%;
    height: 40px
  }
  .details_card {
    overflow: auto;
    padding-left: 20px;
    padding-right: 20px;
    box-sizing: border-box;
    border: 1px solid #f2f2f2;
    border-radius: 4px;
  }

  .details_card:hover {
    box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04)
  }
  .app {
    background-color: rgba(248, 248, 248, 0.8);
  }

  /deep/.el-header {
    padding: 10px;
    /* height: 100% !important; */
    padding-bottom: 0;
  }

  .report_header {
    width: 100%;
    min-height: 40px;
    box-sizing: border-box;
    background-color: rgba(248, 248, 248, 0.8);
    display: flex;
    align-items: center;
    padding-bottom: 5px;
    position: relative;
    padding-left: 5px;
    padding-right: 5px;
  }
  .report_header_left {
    width: 5px;
    height: 100%;
    background-color: #409EFF;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
  }

  .report_header_query {
    margin-left: 10px;
    width: 180px;
    margin-top: 5px;
  }
  .report_header_date {
      margin-left: 10px;
      width: 250px;
      margin-top: 5px;
    }
</style>
